import {
    getEnterpriseList,
    enterpriseRegister,
    enterpriseAudit,
    enterpriseBan,
    enterpriseDetail
  } from './enterpriseListServ.js'
  import { parse } from 'qs'
  import { message } from 'antd'
  import moment from 'moment'
  import { isEmpty, isArray, cloneDeep, merge, get, set, concat, pick } from 'lodash'
  import { hashHistory } from 'dva/router'
  import AppBus from 'utils/bus'
  
  const defultState = {
    loading: false,
    btnLoading: false,
    showAddVisible: false,
    showAuditDialog: false,
    actionType: '',//edit编辑，detail详情，add新增
    enterpriseList:[],
    enterpriseDetail:{},
    remark:"",
    currentId:null,
    dataSource: [],
    modalPageData: {
      pageSize: 10,
      pageNo: 1
    },
  }
  
  const tmpModule = {
    state: cloneDeep(defultState),
    subscriptions: {
      setup ({dispatch, history}) {
        history.listen(({pathname, query}) => {
          if ('' + tmpModule.pathname === '' + pathname) {
            //每次进来页面更新数据 重置state
            dispatch({type: 'updateStore', payload: {...cloneDeep(defultState),...query}}) 
            if (query.actionType == 'edit'||query.actionType == 'check') {
              // 获取详情
              dispatch({type: 'getDetail', payload: {...query}})
            }
          }
        })
      },
    },
    
    effects: {
      // 获取详情
      * getDetail ({payload}, {put, call, select}) {
        let {enterpriseDetail, type} = yield select(state => state[tmpModule.namespace])
        try {
          let {data, success} = yield call(enterpriseDetail, parse({id: payload.id}))
          if (success) {
            enterpriseDetail = data
          }
          yield put({type: 'updateStore', payload: {...payload, enterpriseDetail}})
        } catch (e) {
          console.error(e)
        }
      },
      * postAudit ({payload}, {put, call, select}) {
        let {enterpriseDetail} = yield select(state => state[tmpModule.namespace])  
        try {
          let {success} = yield call(enterpriseAudit, payload.params)
          if (success) {
            enterpriseDetail.remark = payload.remark
          }
          yield put({type: 'updateStore', payload: {...payload, enterpriseDetail}})
        } catch (e) {
          console.error(e)
        }
      },
      * banEnterprise ({payload}, {put, call, select}) {
        let {enterpriseDetail} = yield select(state => state[tmpModule.namespace])  
        try {
          let {success} = yield call(enterpriseBan, payload.params)
          if (success) {
            enterpriseDetail.state = payload.state
          }
          yield put({type: 'updateStore', payload: {...payload, enterpriseDetail}})
        } catch (e) {
          console.error(e)
        }
      },
      * updateAuditDialogState({payload}, {put, call, select}){
        let { showAuditDialog } = payload
        yield put({type: 'updateStore', payload: {showAuditDialog}})
      },
      * selectedKeysChange ({payload}, {put, select}) {
        let {subElements, treeInfo, curSubElements} = yield select(state => state[tmpModule.namespace])
        let {isLeaf, leafId} = treeInfo
        let {selectedRowKeys = []} = payload
        curSubElements = get(subElements, `${leafId}`, {})
        curSubElements.selectedRowKeys = selectedRowKeys
        yield put({type: 'updateStore', payload: {subElements, curSubElements}})
      },
    },
    reducers: {
      updateStore (preState, action) {
        return Object.assign({}, preState, action.payload)
      },
    }
  }
  
  export default tmpModule
  